package com.cirnoworks.uhs.server.impl;

import java.util.HashMap;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import com.cirnoworks.common.Hasher;
import com.cirnoworks.uhs.server.User;
import com.cirnoworks.uhs.server.UserKey;
import com.cirnoworks.uhs.server.UserManager;

@Singleton
public class UserManagerMockImpl implements UserManager {

	private HashMap<UserKey, User> users = new HashMap<UserKey, User>();

	private void addUser(Hasher hasher, String userName, String password) {
		User user = new User();
		user.setUserName(userName);
		UserKey key = new UserKey(hasher.encode(userName + password + ".!@"));
		user.setKey(key);
		users.put(key, user);
	}

	@Inject
	void setupUsers(Hasher hasher, @Named("uhs.server.users") String users) {
		String[] userStrs = users.split(",");
		for (String userStr : userStrs) {
			String[] splittedUserStr = userStr.trim().split(":", 2);
			if (splittedUserStr.length > 1) {
				addUser(hasher, splittedUserStr[0], splittedUserStr[1]);
			}
		}
	}

	@Override
	public User getUser(UserKey key) {
		return users.get(key);
	}

}
